package com.mikufufu.blog.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mikufufu.blog.model.dto.admin.RoleDto;
import com.mikufufu.blog.model.entity.Role;
import com.mikufufu.blog.service.RoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mikufufu.blog.utils.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.mikufufu.blog.common.entity.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@RestController
@RequestMapping("/admin/role")
@Api(tags = "角色接口")
public class RoleAdminController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/detail")
    @ApiOperation(value = "获取角色详情")
    @ApiImplicitParam(name = "id", value = "角色id", required = true)
    public  AjaxResult<Role> detail(Integer id) {
        return AjaxResult.data(roleService.getById(id));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取角色列表")
    public AjaxResult<List<Role>> list() {
        return AjaxResult.data(roleService.list());
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询角色列表")
    public AjaxResult<IPage<Role>> page(@RequestBody RoleDto role) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(role.getName())){
            queryWrapper.like(Role::getRoleName, role.getName());
        }
        if (StringUtils.isNotBlank(role.getCode())){
            queryWrapper.like(Role::getRoleCode, role.getCode());
        }
        return AjaxResult.data(roleService.page(role.getPage(), queryWrapper));
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增角色")
    public AjaxResult<Boolean> add(@RequestBody Role role) {
        return AjaxResult.data(roleService.save(role));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改角色")
    public AjaxResult<Boolean> update(@RequestBody Role role) {
        return AjaxResult.data(roleService.updateById(role));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除角色")
    @ApiImplicitParam(name = "id", value = "角色id", required = true)
    public AjaxResult<Boolean> delete(Integer id) {
        return AjaxResult.data(roleService.removeById(id));
    }

}
